// 初始化
getRecordCategory();
getGoodsWord();
// getRecordListByCategory();

let u_id = 'BN2482';
let recordTab = 1;
let goodsWordHtml = ``
// let communityListHtml = ``
let communityNavIdList = []


$(function () {
  // 初始化
  $('.community-nav .li').eq(recordTab).addClass('current');
  
  $('.community-nav').delegate('.li', 'click', function() {
    $(this).addClass('current').siblings().removeClass('current');
    if ($(this).index() !== recordTab) {
      recordTab = $(this).index()
      showCommunityList(recordTab);
    }
  })

});

// 数据计算

function getAge(date) {
  let birthday = new Date(date).getTime()
  let diff = new Date().getTime() - birthday
  let year = Math.floor(diff / 31536000000)
  let month = Math.floor((diff % 31536000000) / 2592000000)
  if (year == 0) {
    return `${month}个月`
  }
  return `${year}岁${month}个月`
}

function createCommunityNavIdList(list) {
  list.forEach((item) => {
    communityNavIdList.push(item.id)
  })
}

// 创建DOM

function createCommunityNav(list) {
  let navHtml = ``;
  list.forEach((item) => {
    navHtml += `
      <div class="li">
        ${item.name}
      </div>
    `
  });
  $('.community-nav').append(navHtml)
  let communityNavScroll = new IScroll('#community-nav-wrapper', {
    scrollX: true
  });
}

function createGoodsWord(data) {
  goodsWordHtml += `
    <div class="goodsword">
      <div class="goodsword-title">
        ${data.title}
      </div>
      <div class="goodsword-list-wrapper">
        <div class="goodsword-list">
  `;
  data.list.forEach((item) => {
    goodsWordHtml += `
      <div class="li">
        <div class="author">
          <img class="avatar" src="${item.image}">
          <div class="name">${item.author}</div>
        </div>
        <div class="content">
          <div class="qt-icon">
            <i class="iconfont icon-quotation_marks"></i>
          </div>
          <div class="intro">
            <p>${item.content}</p>
            <a class="more">查看全部</a>
          </div>
        </div>
      </div>
    `
  })
  goodsWordHtml += `
        </div>
      </div>
    </div>
  `
}

function createStar(score) {
  let diff = 5 - score
  let star = ``
  for(let i = 0; i < score; i++) {
    star += `
      <div class="star active">
        <i class="iconfont icon-star"></i>
      </div>
    `
  }
  if (diff > 0) {
    for(let j = 0; j < diff; j ++) {
      star += `
        <div class="star">
          <i class="iconfont icon-star"></i>
        </div>
      `
    }
  }
  return star
}

function createCommunityList(list){
  let communityListHtml = ``
  communityListHtml += `
    <div class="community-list">
  `;
  list.forEach((item) => {
    communityListHtml += `
    <div class="community-li">
      <div class="user-header">
        <div class="avatar">
    `
    if (item.userinfo.avatarurl == 'undefined') {
      communityListHtml += `
          <img src="images/img-error-new.jpg">
      `
    } else {
      communityListHtml += `
          <img src="${item.userinfo.avatarurl}">
      `
    }
    communityListHtml += `
          <div class="vip">
            <i class="iconfont icon-hebingxingzhuang"></i>
          </div>
        </div>
        <div class="intro">
          <div class="user-name">${item.userinfo.group}</div>
          <div class="desc">
            <span>${getAge(item.userinfo.birthday)}</span>
            <span>已伴读${item.userinfo.from_days}天</span>
          </div>
        </div>
        <div class="follow">
          <i class="iconfont icon-add"></i>
          <span>关注</span>
        </div>
      </div>
      <h3 class="community-title">${item.title}</h3>
      <p class="community-text">${item.notes}</p>
    `
    // 如果是书
    if (item.c_type == 2) {
      communityListHtml += `
        <div class="book-list-wrapper">
          <div class="book-list">
      `
      item.book.forEach((book) => {
        communityListHtml += `
            <div class="book-li">
              <div class="cover">
                <img src="${book.images}">
        `
        if (book.clock_link_cnt !== 0) {
          communityListHtml += `
                <div class="count">${book.clock_link_cnt}次</div>
          `
        }
        communityListHtml += `
              </div>
              <div class="star-cont">
              ${createStar(book.score)}
              </div>
            </div>
        `
      })
      communityListHtml += `
          </div>
        </div>
      `
    } else if (item.images.length > 0) {
      // 如果是单个图片
      if (item.images.length == 1) {
        communityListHtml += `
        <div class="multi-graph img1">
        `
      } else {
        communityListHtml += `
        <div class="multi-graph">
        `
      }
      item.images.forEach((img) => {
        communityListHtml += `
          <div class="img-cont">
            <img class="img"
              src="${img.url}">
          </div>
      `
      })
      communityListHtml += `
        </div>
      `
    }
    communityListHtml += `
        <div class="space-line"></div>
      </div>
    `
  })
  communityListHtml += `
    </div>
  `
  $('.community').append(communityListHtml)
  let communityScroll = new IScroll('.community-wrapper');
}

function showCommunityList(index) {
  if (index === 0) {
    getSelectedByUid()
  } else if (index === 1) {
    getRecordListByCategory()
  } else {
    getRecordListByCategory(communityNavIdList[index - 2])
  }
  $('.community').replaceWith(`
    <div class="community">
    </div>
  `)
  let communityHtml = ``;
  if (index === 1) {
    communityHtml += goodsWordHtml
  }
  // communityHtml += communityListHtml
  $('.community').append(communityHtml)
  if (index === 1) {
    let goodsWordScroll = new IScroll('.goodsword-list-wrapper', {
      scrollX: true
    });
  }
}

// 获取数据

function getRecordCategory() {
  getData('/v1/plan/getRecordCategory', {
    page: 1,
    size: 10,
  }).then((data) => {
    console.log(data)
    createCommunityNavIdList(data.data)
    createCommunityNav(data.data)
  }).catch((error) => {
    // console.log('获取社区手记分类请求错误：')
    // console.log(error)
    throw new Error(error)
  });
}

function getGoodsWord() {
  getData('/v1/plan/getGoodsWord', {
    page: 1,
    size: 6,
  }).then((data) => {
    createGoodsWord(data.data);
    showCommunityList(recordTab);
  }).catch((error) => {
    throw new Error(error)
  });
}

function getRecordListByCategory(id) {
  getData('/v1/plan/getRecordListByCategory', {
    category_id: id,
    page: 1,
    size: 10,
  }).then((data) => {
    console.log(data)
    createCommunityList(data.data);
  }).catch((error) => {
    throw new Error(error)
  });
}

function getSelectedByUid(uid) {
  getData('/v1/moment/selectedByUid', {
    id: uid,
    page: 1,
    size: 10,
  }).then((data) => {
    console.log(data)
    createCommunityList(data.data);
  }).catch((error) => {
    throw new Error(error)
  });
}
